import { LoginProps } from '@/pages/auth/service';
import downloadCors from '@/utils/downloadCors';
import { request, requestInstance } from '@/utils/request';

export interface MessageType {
  title: string;
  content: string;
}

export interface CsrfChangePwd {
  username: string;
  csrfToken?: string;
  oldPassword?: string;
  newPassword: string;
}

/* XSS */
export const getMessage = async () => await request('GET', '/message').then(res => res.data);

export const addMessage = async (params: MessageType) =>
  await request('POST', '/message', params).then(res => res.data);

export const removeMessage = async () => await request('DELETE', '/message').then(res => res.data);

/* RCE */
export const ping = async (ip: string) =>
  await request('POST', '/rce/ping', { ip }).then(res => res.data);

export const exec = async (command: string) =>
  await request('POST', '/rce/exec', { command }).then(res => res.data);

/* CSRF */
export const changePwdLow = async (params: CsrfChangePwd) =>
  await request('POST', '/csrf/low/changePassword', params).then(res => res.data);

export const changePwdMid = async (params: CsrfChangePwd) =>
  await request('POST', '/csrf/mid/changePassword', params).then(res => res.data);

export const changePwdHigh = async (params: CsrfChangePwd) =>
  await request('POST', '/csrf/high/changePassword', params).then(res => res.data);

/* SQLI */
export const queryUserByUsername = async (params: any) =>
  await request('GET', '/sqli/string/queryUser', params).then(res => res.data);

export const queryUserById = async (params: any) =>
  await request('GET', '/sqli/number/queryUser', params).then(res => res.data);

/* FILE */
export const download = async (filename: string) =>
  await requestInstance({
    method: 'GET',
    url: '/fileDownload',
    responseType: 'blob',
    params: { filename },
  }).then(res => {
    downloadCors(res.data, filename);
  });

/* BURST */
export const dangerousLogin = async (params: LoginProps) =>
  await request('POST', '/burst/normal', params).then(res => res.data);

export const delayLogin = async (params: LoginProps) =>
  await request('POST', '/burst/delay', params).then(res => res.data);

export const limitLogin = async (params: LoginProps) =>
  await request('POST', '/login', params).then(res => res.data);
